/**
 ******************************************************************************
 * @file    main.c
 * @author  jianqiang.xue
 * @version V1.0.0
 * @date    2023-08-16
 * @brief   main function
 ******************************************************************************
 */
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "os_api.h"
#include "main.h"
// M0没有位操作区

/* Private variables ---------------------------------------------------------*/

OS_THREAD_DECLARE(app_main, 128);
OS_THREAD_DECLARE(app_lisun, 128);

// 测试队列
typedef struct {
    uint8_t i;
    uint8_t n;
} lisun_queue_t;
OS_MSG_DECLARE(lisun, 3, lisun_queue_t);
// int g_stack1[64 / sizeof(int)];
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/

OS_THREAD_FUNC(app_main) {
    lisun_queue_t test;
    while (1) {
        if (OS_MSG_NO_NULL(lisun)) {
            OS_MSG_DEQUEUE_MSG(lisun, &test);
            printf("rx:%02x,%02x\r\n", test.i, test.n);
        }

    }
}

OS_THREAD_FUNC(app_lisun) {
    OS_MSG_CREATE(lisun, 3, lisun_queue_t);
    lisun_queue_t test;
    test.i = 0;
    test.n = 0xff;
    while (1) {
        test.i++;
        test.n--;
        OS_MSG_ENQUEUE_MSG(lisun, &test);
        BSP_LED_Toggle(LED1);
        os_delay(1000);
    }
}

OS_THREAD_FUNC(main) { // 本线程使用的是系统栈，不是线程栈，请注意
    (void)arg;

    /* create two sub tasks */
    OS_THREAD_CREATE(app_main);
    OS_THREAD_CREATE(app_lisun);
    /* wait two sub tasks return */
    OS_THREAD_WAIT(app_main);
    while(1); // 执行到这，代码主线程退出了
}

int main(void) {
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock to HIRC 24MHz*/
    SystemClock_Config();

    /* Initialize BSP Led for LED1 */
    BSP_LED_Init(LED1);

    /* Configure uart1 for printf */
    LogInit();

    os_kernel_initialize();
    os_kernel_start();

    while (1) {
        //    BSP_LED_Toggle(LED1);
        //		GPIOD->ODR  ^= LED1_PIN; // 500 ns

        //		GPIOD->ODSET = LED1_PIN; // 置位1 效率更高 83 ns
        //		GPIOD->ODCLR = LED1_PIN; // 置位0

        //		PDout(4) = 1; // 500 NS
        //		PDout(4) = 0; //
        //    HAL_Delay(1000);
    }
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void) {
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HIRC;
    RCC_OscInitStruct.HIRCState = RCC_HIRC_ON;
    RCC_OscInitStruct.HIRCCalibrationValue = RCC_HIRCCALIBRATION_24M;

    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }

    /**Initializes the CPU, AHB and APB busses clocks
     */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HIRC;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APBCLKDivider = RCC_PCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK) {
        Error_Handler();
    }
}

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void) {
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */

    /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t* file, uint32_t line) {
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/* Private function -------------------------------------------------------*/
